package detect;

import interfaces.Detect;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import org.jsoup.nodes.Document;

import com.google.common.reflect.TypeToken;
import com.google.gson.Gson;

import entities.Domain;
import entities.Widget;

public class DetectWidgetTwitterTweetButton implements Detect {
	@SuppressWarnings("serial")
	@Override
	public void getInfo(Map<String, Object> obj) {
		// TODO Auto-generated method stub
		Document doc = (Document) obj.get("doc");
		Domain domain = (Domain) obj.get("domain");
		Type listType = new TypeToken<List<Widget>>() {
		}.getType();
		List<Widget> widgets = new Gson()
				.fromJson(domain.getWidget(), listType);

		if (widgets == null) {
			widgets = new ArrayList<Widget>();
		}

		try {
			Widget widget = new Widget();

			String widgetName = "";

			if (doc.select(".twitter-share-button").size() > 0
					|| doc.select(
							"script[src*=http://platform.twitter.com/widgets.js]")
							.size() > 0) {
				widgetName = "Twitter Tweet Button";
			}

			if (!widgetName.isEmpty()) {
				widget.setN(widgetName);
				widgets.add(widget);
			}

			domain.setWidget(new Gson().toJson(widgets));
		} catch (Exception e) {
			// TODO: handle exception
		}
	}
}
